<!--
 * @Author: your name
 * @Date: 2021-06-24 16:12:49
 * @LastEditTime: 2021-06-28 09:34:25
 * @LastEditors: Please set LastEditors
 * @Description: 资讯分类管理
 * @FilePath: \briup\项目\briup-ui\src\pages\admin\info\category\List.vue
-->
<template>
  <!-- 资讯分类管理 -->
  <div>
    <div class="header">
      <el-button type="primary" @click="toAdd">新增</el-button>
    </div>
    <div class="table">
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" align="center" width="50" />
        <el-table-column prop="name" label="名称" align="center" width="100" />
        <el-table-column prop="no" label="排序号" align="center" width="100" />
        <el-table-column prop="description" label="描述" align="center" />
        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              class="green_info"
              type="text"
              size="small"
              @click="toEdit(scope.row)"
            >编辑</el-button>
            <el-button
              class="red_info"
              type="text"
              size="small"
              @click="toDelete(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="title" :visible.sync="visible" @close="toClear">
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-form-item
          label="栏目名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth" prop="no">
          <el-input v-model="form.no" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="描述"
          :label-width="formLabelWidth"
          prop="description"
        >
          <el-input
            v-model="form.description"
            autocomplete="off"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  batchDeletecategory,
  findAllcategory,
  saveOrUpdatecategory
} from '@/api/infoCategory'
export default {
  data() {
    return {
      tableData: [],
      loading: false,
      form: {},
      title: '',
      formLabelWidth: '80px',
      visible: false,
      rules: {
        name: [
          { required: true, message: '请输入轮播图名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.findAlltableData()
  },
  mounted() {},
  methods: {
    async findAlltableData() {
      this.loading = true
      const res = await findAllcategory()
      this.tableData = res.data
      this.loading = false
    },
    toAdd() {
      this.form = {}
      this.title = '新增栏目信息'
      this.visible = true
    },
    toEdit(row) {
      this.form = { ...row }
      this.title = '编辑栏目信息'
      this.visible = true
    },
    toClear() {
      this.$refs.ruleForm.resetFields()
      setTimeout(() => {
        this.visible = false
      }, 100)
    },
    toSave() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          const res = await saveOrUpdatecategory(this.form)
          this.$notify.success({
            title: '成功',
            message: res.message
          })
          this.findAlltableData()
          this.visible = false
        } else {
          return false
        }
      })
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await batchDeletecategory({ id })
          this.$message({ type: 'success', message: res.message })
          this.findAlltableData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.header {
  display: flex;
  .el-button {
    margin-right: 10px;
  }
}
</style>
